import blog.models
from django.contrib.comments import Comment
from django.core.mail import EmailMessage
from django.template.loader import render_to_string
from django.contrib.sites.models import Site
site = Site.objects.get_current()



def resolve_object(comment):
    """
    For using with recursive comments.
    Return object that was commented first
    """
    if isinstance(comment.content_object, Comment):
        return resolve_object(comment.content_object)
    else:
        return comment.content_object



def increment_comment(post):
    if hasattr(post, 'postmeta'):
        post.postmeta.comments_count += 1
        post.postmeta.save()



def post_notify(post):
    """
    Send notification about new post in blog
    """
    subsribers = blog.models.UserNotification.objects.filter(on_new_post=True).exclude(user=post.author)
    bcc = [s.user.email for s in subsribers if s.user.email]
    if bcc:
        subject = render_to_string(
            'email/post_notify_subject.txt', {
                'site': site,
                'title': post.title,
                'username': post.author,
            }
        )
        body = render_to_string(
            'email/post_notify_body.txt', {
                'site': site,
                'title': post.title,
                'username': post.author,
                'post': post.short_body,
                'url': post.get_absolute_url(),
            }
        )
        notify = EmailMessage(
            subject = subject,
            body = body,
            bcc = bcc
        )
        notify.send()



def comment_notify(post, comment):
    """
    Send notification about new comment in thread
    """
    subsribers = blog.models.UserNotification.objects.filter(posts=post.id).exclude(user=comment.user)
    bcc = [s.user.email for s in subsribers if s.user.email]
    if bcc:
        subject = render_to_string(
            'email/comment_notify_subject.txt', {
                'site': site,
                'title': post.title,
                'username': comment.user_name,
            }
        )
        body = render_to_string(
            'email/comment_notify_body.txt', {
                'site': site,
                'title': post.title,
                'username': comment.user_name,
                'comment': comment.comment,
                'url': comment.get_absolute_url(),
            }
        )
        notify = EmailMessage(
            subject = subject,
            body = body,
            bcc = bcc
        )
        notify.send()



def answer_notify(post, comment):
    """
    Send notification about answer on comment
    """
    if isinstance(comment.content_object, Comment):
        user = comment.content_object.user
        if user and user != comment.user and user.email:
            notifications = blog.models.UserNotification.objects.get(user=user.id)
            if notifications.on_my_comments:
                to = user.email
                subject = render_to_string(
                    'email/answer_notify_subject.txt', {
                        'site': site,
                        'title': post.title,
                        'username': comment.user_name,
                    }
                )
                body = render_to_string(
                    'email/answer_notify_body.txt', {
                        'site': site,
                        'title': post.title,
                        'username': comment.user_name,
                        'comment': comment.comment,
                        'url': comment.get_absolute_url(),
                    }
                )
                notify = EmailMessage(
                    subject = subject,
                    body = body,
                    to = [to]
                )
                notify.send()


